﻿using System;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;

namespace tmAsk.Data
{
    public class Filetemplate
    {
        public static bool createfile(string path)
        {
            try
            {
                if (!System.IO.File.Exists(path))
                {
                    System.IO.File.Create(path).Close();
                    return true;
                }
                System.IO.File.Delete(path);
                System.IO.File.Create(path).Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string getMd5Hash(string input)
        {
            byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x2"));
            }
            return builder.ToString();
        }

        public static bool isfile(string files)
        {
            if (!System.IO.File.Exists(files))
            {
                return false;
            }
            return true;
        }

        public static string readfiles(string url)
        {
            string str2;
            string path = url;
            try
            {
                StreamReader reader = new StreamReader(path, Encoding.UTF8);
                str2 = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            return str2;
        }

        public static string replacehtmls(DataRow r, string value, string html_type, int i)
        {
            string str = value;
            foreach (DataColumn column in r.Table.Columns)
            {
                str = str.Replace("{" + column.ToString() + "}", r[column].ToString());
                str = str.Replace("[itemid]", i.ToString());
            }
            return str;
        }

        public static string replacehtmls(DataRow r, string value, string html_type, int i, int num)
        {
            string str = value;
            foreach (DataColumn column in r.Table.Columns)
            {
                str = str.Replace("{<" + column.ToString() + ">}", r[column].ToString());
                str = str.Replace("[<itemid>]", i.ToString());
            }
            return str;
        }

        public static bool SendEmail(string to, string from, string fromname, string subject, string body, string userName, string password, string smtpHost, int port)
        {
            try
            {
                MailAddress address = new MailAddress(from, fromname);
                MailAddress address2 = new MailAddress(to);
                MailMessage message = new MailMessage(address, address2);
                message.Subject = subject;
                message.IsBodyHtml = true;
                message.Body = body;
                SmtpClient client = new SmtpClient(smtpHost, port);
                client.Credentials = new NetworkCredential(userName, password);
                client.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool verifyMd5Hash(string input, string hash)
        {
            string x = getMd5Hash(input);
            StringComparer ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase;
            return (0 == ordinalIgnoreCase.Compare(x, hash));
        }

        public static bool writehtml(string url, string value)
        {
            try
            {
                if (!isfile(url))
                {
                    createfile(url);
                }
                StreamWriter writer = new StreamWriter(url, false, Encoding.UTF8);
                writer.Write(value);
                writer.Flush();
                writer.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

}